package com.pb.infra.auth.controller;

import com.pb.infra.auth.entity.pbsys.PriviMenuInterface;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.entity.common.Tree;
import com.pb.infra.auth.entity.custom.PriviMenuCustom;
import com.pb.infra.auth.service.PriviMenuService;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * 菜单管理
 *
 * @author liudebao
 * @date 2020/1/1
 */
@Slf4j
@RestController
@RequestMapping("v1/system/menu")
public class PriviMenuController {

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "privi_menu";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "菜单管理";

    /**
     * 业务逻辑对象
     */
    @Resource
    private PriviMenuService priviMenuService;

    /**
     * 读取全量菜单信息
     *
     * @return List<Tree> 全量菜单信息
     */
    @GetMapping("select")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result select(String appId) {
        return Result.ok(priviMenuService.select(appId));
    }


    /**
     * 读取某个菜单信息
     *
     * @param priviMenu 菜单编号
     * @return PriviMenuCustom 某个菜单信息
     */
    @GetMapping("getMenuById")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result getMenuById(PriviMenuCustom priviMenu) {
        return Result.ok(priviMenuService.getMenuById(priviMenu));
    }

    /**
     * 新建菜单
     *
     * @param priviMenu 请求参数
     * @return Result 返回数据
     */
    @PostMapping("insert")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result insert(@RequestBody PriviMenuCustom priviMenu, HttpServletRequest request) {
        if (Constant.OK.equals(priviMenu.getParentId()) && "true".equals(priviMenu.getHideMenus())) {
            priviMenu.setHideFlag(Constant.EXIST);
        } else {
            priviMenu.setHideFlag(null);
        }
        return priviMenuService.insert(priviMenu, request);
    }

    /**
     * 修改菜单
     *
     * @param priviMenu 请求参数
     * @return Result 返回数据
     */
    @PostMapping("update")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result update(@RequestBody PriviMenuCustom priviMenu, HttpServletRequest request) {
        if (Constant.OK.equals(priviMenu.getParentId()) && "true".equals(priviMenu.getHideMenus())) {
            priviMenu.setHideFlag(Constant.EXIST);
        } else {
            priviMenu.setHideFlag(null);
        }
        priviMenuService.update(priviMenu, request);
        return Result.OK_EMPTY;
    }

    /**
     * 删除菜单
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("delete")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result delete(@RequestBody Map<String, Object> paramMap) {
        priviMenuService.delete(paramMap);
        return Result.OK_EMPTY;
    }

    /**
     * 查询菜单对应接口
     * @param paramMap 请求参数
     * @param page 请求参数
     * @return Result 返回数据
     */
    @GetMapping("getMenuInters")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result getMenuInters(@RequestParam Map<String, Object> paramMap, Page page) {
        return priviMenuService.getMenuInters(paramMap, page);
    }

    /**
     * 新增菜单接口
     * @param priviMenu 请求参数
     * @param request
     * @return Result 返回数据
     */
    @PostMapping("insertInter")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result insertInter(@RequestBody PriviMenuInterface priviMenu, HttpServletRequest request) {
        priviMenuService.insertInter(priviMenu, request);
        return Result.OK_EMPTY;
    }

    /**
     * 修改菜单接口
     *
     * @param priviMenu 请求参数
     * @return Result 返回数据
     */
    @PostMapping("updateInter")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result updateInter(@RequestBody PriviMenuInterface priviMenu) {
        priviMenuService.updateInter(priviMenu);
        return Result.OK_EMPTY;
    }

    /**
     * 删除菜单接口
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("deleteInter")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result deleteInter(@RequestBody List<Map<String, Object>> paramMap) {
        priviMenuService.deleteInter(paramMap);
        return Result.OK_EMPTY;
    }

    /**
     * 修改菜单拖拽位置
     * @param paramMap 请求参数
     * @return Result 返回数据0
     */
    @PostMapping("updateMenuPath")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result updateMenuPath(@RequestBody Map<String, Object> paramMap) {
        priviMenuService.updateMenuPath(paramMap);
        return Result.OK_EMPTY;
    }

    @GetMapping("queryAllInter")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result queryAllInter() {
        return priviMenuService.queryAllInter();
    }

}
